﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Infrastructure.Core.IOC;
using SMG.ServiceContract.Program;

namespace ManageForm
{
    public partial class BillListForm : Form
    {
        //节目单服务
        IProgramBillService billService = ServiceLocator.Instance.Resolve<IProgramBillService>();
        //频道服务
        IChannelService channelService = ServiceLocator.Instance.Resolve<IChannelService>();

        public BillListForm()
        {
            InitializeComponent();
        }

        #region Control Events

        private void BillListForm_Load(object sender, EventArgs e)
        {
            InitFormData();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            SetBillTemplateForm form = new SetBillTemplateForm();
            form.ShowDialog();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            BillResourcesForm form = new BillResourcesForm();
            form.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadBillVersionList();
        }

        private void dgBillVersionView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0) {
                var id = this.dgBillVersionView[0, e.RowIndex].Value;
                if (id != null)
                    LoadBillList(id.ToString());
            }            
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        void InitFormData() {
            InitQueryCondition();
            
            //LoadBillVersionList();
        }
        /// <summary>
        /// 初始化查询条件
        /// </summary>
        void InitQueryCondition() {
            this.dtkPlayDate.Format = DateTimePickerFormat.Custom;
            this.dtkPlayDate.CustomFormat = "yyyy-MM-dd";
            this.dtkPlayDate.Value = DateTime.Now.AddDays(-10);
            var channels = channelService.QueryChannelAll();
            this.cbChannel.DataSource = channels;
            this.cbChannel.ValueMember = "ChannelId";
            this.cbChannel.DisplayMember = "ChannelName";
        }
        /// <summary>
        /// 加载节目单版本列表信息
        /// </summary>
        void LoadBillVersionList() {
            var channelId = this.cbChannel.SelectedValue.ToString();
            var playDate = this.dtkPlayDate.Value.ToString("yyyy-MM-dd");
            this.dgBillVersionView.AutoGenerateColumns = false;

            //查询节目单版本列表
            var billVersions = billService.QueryBillVersion(channelId, playDate);
            this.dgBillVersionView.DataSource = billVersions;
            
            if (billVersions != null && billVersions.Any()) {
                //绑定节目单列表
                var versionId = this.dgBillVersionView[0, 0].Value;
                if (versionId != null)
                    LoadBillList(versionId.ToString());
            }            
        }
        /// <summary>
        /// 绑定节目单列表信息
        /// </summary>
        void LoadBillList(String versionId) {
            long vid;
            if (long.TryParse(versionId, out vid)) {
                var bills = billService.QueryBillByVersion(vid);
                this.dgBillView.DataSource = bills;
            }
        }

        #endregion

    }
}
